module.exports={
    configureWebpack:{
        devServer:{
            before(app){
                //编写数据
                let tokenkey='lalala';
                //用户池
                const userPoor=[
                    {
                        username:'张丽',
                        password:'12345'
                    },
                    {
                        username:'赵丽颖',
                        password:'12345'
                    }
                ]
                //用户登录接口
                app.get('/api/login',(req,res)=>{
                    let {username,password}=req.query;
                    if(username=='张丽'&&password=='12345'||username=='赵丽颖'&&password=='12345'){
                        res.json({
                            code:0,
                            message:'登录成功！',
                            token:tokenkey+'-'+username+'-'+(new Date().getTime()+60*60*1000)
                        })
                    }else{
                        res.json({
                            code:1,
                            message:'账号或者密码错误！'
                        })
                    }
                })
                //左侧菜单
                app.get('/api/menu',(req,res)=>{
                    res.json({
                        "status": "ok",
                        "data": [
                            {
                            "id": 1,
                            "name": "用户管理",
                            "child": [
                                {
                                "id": 1,
                                "name": "用户列表",
                                "path": "/users"
                                }
                            ]
                            }, 
                            {
                            "id": 2,
                            "name": "权限管理",
                            "child": [
                                {
                                "id": 2,
                                "name": "角色列表",
                                "path": "/rote"
                                },
                                {
                                "id": 3,
                                "name": "权限列表",
                                "path": "/right"
                                }
                            ]
                            }, 
                            {
                            "id": 3,
                            "name": "商品管理",
                            "child": [
                                {
                                "id": 4,
                                "name": "商品列表",
                                "path": "/product"
                                }, 
                                {
                                "id": 5,
                                "name": "分类参数",
                                "path": "/userss"
                                }, 
                                {
                                "id": 6,
                                "name": "商品分类",
                                "path": "/producttype"
                                }
                            ]
                            },
                            {
                            "id": 4,
                            "name": "订单管理",
                            "child": []
                            },
                            {
                            "id": 5,
                            "name": "数据统计",
                            "child": []
                            }
                        ]
                    })
                })
                //模拟用户列表
                var userlist = [{
                    "id": 10,
                    "UserName": "deng",
                    "Password": "123456",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "",
                    "Email": "",
                    "MgState":false
                }, {
                    "id": 9,
                    "UserName": "admib",
                    "Password": "123456",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "",
                    "Email": "",
                    "MgState":true
                }, {
                    "id": 8,
                    "UserName": "deng",
                    "Password": "123456",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "",
                    "Email": "",
                    "MgState":false
                }, {
                    "id": 7,
                    "UserName": "5555",
                    "Password": "555555",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "13312311231",
                    "Email": "55@qq.com",
                    "MgState":false
                }, {
                    "id": 6,
                    "UserName": "3333",
                    "Password": "333333",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "13388888888",
                    "Email": "33@qq.com",
                    "MgState":false
                }, {
                    "id":5,
                    "UserName": "2222",
                    "Password": "222222",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "13845644564",
                    "Email": "22@qq.com",
                    "MgState":false
                }, {
                    "id": 4,
                    "UserName": "deng",
                    "Password": "123456",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "",
                    "Email": "",
                    "MgState":true
                }, {
                    "id": 3,
                    "UserName": "den",
                    "Password": "",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "13812341235",
                    "Email": "888888@qq.com",
                    "MgState":true
                }, {
                    "id": 2,
                    "UserName": "admin",
                    "Password": "",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "13812341888",
                    "Email": "288888@qq.com",
                    "MgState":true
                }, {
                    "id": 1,
                    "UserName": "deng",
                    "Password": "123456",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "超级管理员",
                    "Mobile": "13602221234",
                    "Email": "223456@qq.com",
                    "MgState":true
                }];
                //模拟用户列表信息+分页
                app.get('/api/users', (req,res) => {
                    const { query, pagenum,pagesize}=req.query
                    var  total=0;
                    var  newDataList=[];
                    if(query!=''){
                        total=userlist.filter(val=>val.UserName==query).length;
                        if(pagenum==0){
                            newDataList=userlist.filter(val=>val.UserName==query).slice(pagenum*pagesize,(parseInt(pagenum)+1)*pagesize)
                        }else{
                            newDataList=userlist.filter(val=>val.UserName==query).slice((parseInt(pagenum)-1)*pagesize,pagenum*pagesize)
                        }
                        res.json({
                            'status':'ok',
                            'total':total,
                            'data':newDataList
                        })
                    }else{
                        total=userlist.length;
                        if(pagenum==0){
                            newDataList=userlist.slice(pagenum*pagesize,(parseInt(pagenum)+1)*pagesize)
                        }else{
                            newDataList=userlist.slice((parseInt(pagenum)-1)*pagesize,pagenum*pagesize)
                        }
                        res.json({
                            'status':'ok',
                            'total':total,
                            'data':newDataList
                        })
                    }
                })
                //模拟用户更新接口
                app.get('/api/userupdate',(req,res)=>{
                    const {id,MgState}=req.query;
                    var count=0;
                    var newState=''
                    if(userlist){
                        var user=userlist.filter(u=>u.id==id);
                        count=user.length;   //获取到1条，如果没有就是0条
                        user.MgState=MgState;
                        newState=user.MgState;
                    }
                    res.json({
                        'status':'ok',
                        'count':count,
                        'newState':newState
                    })
                })
                //添加用户的接口
                app.get('/api/adduser',(req,res)=>{
                    const {name,password,Email,tel,pagenum,pagesize}=req.query;
                    var id=userlist[0].id;
                    var adduser={
                        "id":id+1,
                        "UserName":name,
                        "Password":password,
                        "CreateTime": "0001/1/1 星期一 0:00:00",
                        "RoleName": "超级管理员",
                        "Mobile":tel,
                        "Email": Email,
                        "MgState":true
                    };
                    userlist.push(adduser);
                    userlist.sort(function(a,b){
                        return b.id-a.id//降序排列
                    });
                    var  total=0;
                    var  newDataList=[];
                    total=userlist.length;
                    if(pagenum==0){
                        newDataList=userlist.slice(pagenum*pagesize,(parseInt(pagenum)+1)*pagesize)
                    }else{
                        newDataList=userlist.slice((parseInt(pagenum)-1)*pagesize,pagenum*pagesize)
                    }
                    res.json({
                        'status':'ok',
                        'total':total,
                        'data':newDataList
                    })
                });
                //修改用户的接口
                app.get('/api/updateUser',(req,res)=>{
                    const {id,name,password,Email,tel,pagenum,pagesize}=req.query;
                    if(userlist){
                        userlist.filter(val=>val.id==id).forEach(e=>{
                            e.UserName=name;
                            e.Email=Email;
                            e.Mobile=tel;
                        });
                        userlist.sort(function(a,b){
                            return b.id-a.id//降序排列
                        });
                        var  total=0;
                        var  newDataList=[];
                        total=userlist.length;
                        if(pagenum==0){
                            newDataList=userlist.slice(pagenum*pagesize,(parseInt(pagenum)+1)*pagesize)
                        }else{
                            newDataList=userlist.slice((parseInt(pagenum)-1)*pagesize,pagenum*pagesize)
                        }
                        res.json({
                            'status':'ok',
                            'total':total,
                            'data':newDataList
                        })
                    }
                });
                //删除用户的接口
                app.get('/api/deluser',(req,res)=>{
                    const {id,query,pagenum,pagesize}=req.query;
                    if(userlist){
                       for(var i=0;i<userlist.length;i++)
                        {
                            if(userlist[i].id==id){
                                userlist.splice(i,1);
                            }
                        }
                    }
                    userlist.sort(function(a,b){
                        return b.id-a.id//降序排列
                    });
                    var  total=0;
                    var  newDataList=[];
                    total=userlist.length;
                    if(pagenum==0){
                        newDataList=userlist.slice(pagenum*pagesize,(parseInt(pagenum)+1)*pagesize)
                    }else{
                        newDataList=userlist.slice((parseInt(pagenum)-1)*pagesize,pagenum*pagesize)
                    }
                    res.json({
                        'status':'ok',
                        'total':total,
                        'data':newDataList
                    })
                })
            }
        }
    }
}